package com.jingwu.example.service

import com.jingwu.example.pojo.entity.UserDO
import com.jingwu.example.repository.UserRepository
import spock.lang.Specification

/**
 * spock init without spring container
 *
 * @author 菁芜
 * @since 2021/11/20 - 16:09
 */
class UserService4Test extends Specification {

    def repository = Mock(UserRepository)
    def service = new UserService()


    def setup() {
        // can use construct inject
        service.setRepository(repository)
    }

    def "test get User"() {
        given:
        def user1 = new UserDO(id: 1, username: "爆爆", age: 8)
        def user2 = new UserDO(id: 2, username: "金克丝", age: 16)

        and:
        repository.selectById(1) >> user1
        repository.selectById(2) >> user2

        when:
        def user = service.getUser(2)

        then:
        with(user) {
            user.username == "金克丝"
            user.age == 16
        }
    }
}
